package org.t236.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sun.deploy.security.BlockedException;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.t236.commons.model.AjaxResult;
import org.t236.commons.model.Order;
import org.t236.commons.model.User;
import org.t236.feign.order.OrderRemoteClient;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RefreshScope
public class UserController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private OrderRemoteClient orderRemoteClient;

//    @Value("${blackIpList}")
    private String blackIpList;
    @Value("${user.id}")
    private String userId;
    @Value("${user.name}")
    private String userName;
    @Value("${user.age}")
    private String userAge;

    @SentinelResource(value = "userInfo", blockHandler = "handleEx")
    @GetMapping("/userInfo")
    public AjaxResult userInfo() {
        return AjaxResult.success(userId + "," + userName + "," + userAge);
    }
    public AjaxResult handleEx(BlockException ex) {
        return AjaxResult.error("/userInfo被限流了:" + ex);
    }

    @GetMapping("/blackIpList")
    public AjaxResult blackIpList() {
        return AjaxResult.success(blackIpList);
    }
    //    @GetMapping("/users/{userId}")
//    public AjaxResult find(@PathVariable("userId") Long userId) {
//
//        User user = users.stream().filter(u -> u.getId().equals(userId)).collect(Collectors.toList()).get(0);
//        AjaxResult ajaxResult =  restTemplate.getForObject("http://localhost:8001/findByUserId?userId=1", AjaxResult.class);
//        user.setOrders((List<Order>) ajaxResult.getData());
//        return AjaxResult.success(user);
//    }

//    @GetMapping("/users/{userId}")
//    public AjaxResult find(@PathVariable("userId") Long userId) {
//        User user = users.stream().filter(u -> u.getId().equals(userId)).collect(Collectors.toList()).get(0);
//        AjaxResult result = restTemplate.getForObject("http://order-service/orders/findByUserId?userId=" + userId, AjaxResult.class);
//        user.setOrders((List<Order>) result.getData());
//        return AjaxResult.success(user);
//    }

    @GetMapping("/users/{userId}")
    public AjaxResult find(@PathVariable("userId") Long userId) {
        User user = users.stream().filter(u -> u.getId().equals(userId)).collect(Collectors.toList()).get(0);
        AjaxResult result = orderRemoteClient.findByUserId(userId);
        if(result.getCode() != AjaxResult.SUCCESS) {
            return AjaxResult.error("获取订单微服务失败" + result.getMsg());
        }
        user.setOrders((List<Order>) result.getData());
        return AjaxResult.success(user);
    }

    @GetMapping("/users")
    public AjaxResult findAll() {
        return AjaxResult.success(users);
    }

    @PostMapping("/users")
    public AjaxResult add(@RequestBody User user) {
        users.add(user);
        return AjaxResult.success(users);
    }

    private static List<User> users = new ArrayList<>();
    static {
        users.add(new User(1L, "张三"));
        users.add(new User(2L, "李四"));
    }
}
